package org.wiztools.commons.feed;

import java.io.Serializable;
import java.net.URL;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author subhash
 */
public final class FeedEntry implements Cloneable, Serializable {

    private URL url;
    private String title;
    private String summary;
    private String content;
    private Date publishedDate;
    private Date updatedDate;
    private Map<String, Serializable> extensions = new HashMap<String, Serializable>();

    public FeedEntry() {}

    public FeedEntry(URL url,
            String title,
            String summary,
            String content,
            Date publishedDate,
            Date updatedDate) {
        this.url = url;
        this.title = title;
        this.summary = summary;
        this.content = content;
        this.publishedDate = publishedDate;
        this.updatedDate = updatedDate;
    }

    public void addExtension(String key, Serializable value){
        extensions.put(key, value);
    }

    public Map<String, Serializable> getExtensions(){
        return Collections.unmodifiableMap(extensions);
    }

    public String getSummary(){
        return summary;
    }

    public void setSummary(String summary){
        this.summary = summary;
    }

    public String getContent(){
        return content;
    }

    public void setContent(String content){
        this.content = content;
    }

    public Date getPublishedDate() {
        if(publishedDate == null){
            return null;
        }
        return new Date(publishedDate.getTime());
    }

    public void setPublishedDate(Date publishedDate) {
        this.publishedDate = publishedDate;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getUpdatedDate() {
        if(updatedDate == null){
            return null;
        }
        return new Date(updatedDate.getTime());
    }

    public void setUpdatedDate(Date updatedDate) {
        this.updatedDate = updatedDate;
    }

    public URL getUrl() {
        return url;
    }

    public void setUrl(URL url) {
        this.url = url;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("<").append(title).append(", ").append(url).append(", ").append(summary == null ? "null" : summary).append(", ").append(content == null ? "null" : content).append(", ").append(publishedDate).append(", ").append(updatedDate).append(">");

        return sb.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final FeedEntry other = (FeedEntry) obj;
        if (this.url != other.url && (this.url == null || !this.url.equals(other.url))) {
            return false;
        }
        if ((this.title == null) ? (other.title != null) : !this.title.equals(other.title)) {
            return false;
        }
        if ((this.summary == null) ? (other.summary != null) : !this.summary.equals(other.summary)) {
            return false;
        }
        if ((this.content == null) ? (other.content != null) : !this.content.equals(other.content)) {
            return false;
        }
        if (this.publishedDate != other.publishedDate && (this.publishedDate == null || !this.publishedDate.equals(other.publishedDate))) {
            return false;
        }
        if (this.updatedDate != other.updatedDate && (this.updatedDate == null || !this.updatedDate.equals(other.updatedDate))) {
            return false;
        }
        if (this.extensions != other.extensions && (this.extensions == null || !this.extensions.equals(other.extensions))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 29 * hash + (this.url != null ? this.url.hashCode() : 0);
        hash = 29 * hash + (this.title != null ? this.title.hashCode() : 0);
        hash = 29 * hash + (this.summary != null ? this.summary.hashCode() : 0);
        hash = 29 * hash + (this.content != null ? this.content.hashCode() : 0);
        hash = 29 * hash + (this.publishedDate != null ? this.publishedDate.hashCode() : 0);
        hash = 29 * hash + (this.updatedDate != null ? this.updatedDate.hashCode() : 0);
        hash = 29 * hash + (this.extensions != null ? this.extensions.hashCode() : 0);
        return hash;
    }

    @Override
    public Object clone() {
        FeedEntry entry = new FeedEntry();
        entry.setTitle(title);
        entry.setSummary(summary);
        entry.setContent(content);
        entry.setUrl(url);
        if(publishedDate != null){
            entry.setPublishedDate(new Date(publishedDate.getTime()));
        }
        if(updatedDate != null){
            entry.setUpdatedDate(new Date(updatedDate.getTime()));
        }
        for(String key: extensions.keySet()){
            entry.addExtension(key, extensions.get(key));
        }
        return entry;

    }
}
